@section('css')
    <link rel="stylesheet" href="{{asset('admin-lte/plugins/datatables/dataTables.bootstrap4.css')}}">
    {{--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">--}}
    <style>
        .form-group{
            display: flex;
        }
        .form-group .control-label{
            text-align:right;
        }
        .portlet>.portlet-title {
            border-bottom: 2px solid #eef1f5;;
        }
        .help-block-error{
            color:red;
        }
        .required{
            color:red;
        }
    </style>
@stop



<div class="row">
    <div class="col-md-12">
        @include('layouts.error')
        <div class="portlet light portlet-fit portlet-form bordered">
            <div class="portlet-title">
                <div class="caption">
                    <i class=" icon-layers font-green"></i>
                    <span class="caption-subject font-green sbold">促销内容</span>
                </div>
            </div>
            <div class="portlet-body">
                <form action="{{$action=='create'?url('admin/coupon/createrule'):url('admin/coupon/editrule')}}" class="form-horizontal" id="validation-form" method="post">
                    <input type="hidden" name="rule_id" value="<?php echo isset($rule_row['rule_id'])?$rule_row['rule_id']:0;?>" class="rule_id">
                    <div class="form-body">
                        <div class="form-group">
                            <label class="control-label col-md-3">标题
                                <span class="required"> * </span>
                            </label>
                            <div class="col-md-3">
                                <input type="text" name="Rule[title]" class="form-control"
                                       value="<?php echo isset($rule_row['title'])?$rule_row['title']:'';?>"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="control-label col-md-3">描述
                                <span class="required"> * </span>
                            </label>
                            <div class="col-md-3">
                                <textarea class="form-control" name="Rule[brief]" rows="3"><?php echo isset($rule_row['brief'])?$rule_row['brief']:'';?></textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="control-label col-md-3">优惠类型
                                <span class="required"> * </span>
                            </label>
                            <div class="col-md-3">
                                <select class="form-control activity-type" name="Rule[reduce_type]">
                                    <option value="subtract" <?php echo isset($rule_row['reduce_type'])&&$rule_row['reduce_type']=='subtract'?'selected':'';?>>满减</option>
                                    <option value="freeship" <?php echo isset($rule_row['reduce_type'])&&$rule_row['reduce_type']=='freeship'?'selected':'';?>>免邮</option>
                                    <option value="discount" <?php echo isset($rule_row['reduce_type'])&&$rule_row['reduce_type']=='discount'?'selected':'';?>>折扣</option>
                                    <!--<option name="gifts">满赠</option>-->
                                </select>
                            </div>
                        </div>
                        <div class="form-group" style="">
                            <div class="col-md-6">
                                <div class="portlet light">
                                    <div class="portlet-title">
                                        <div class="caption">
                                            <i class="icon-speech"></i>
                                            <span class="caption-subject bold uppercase"> 促销规则</span>
                                        </div>
                                    </div>
                                    <div class="portlet-body">
                                        <div data-rail-visible="1" data-rail-color="yellow" data-handle-color="#a1b2bd">
                                            <div class="form-group">
                                                <label class="control-label col-md-3">订单最低金额：</label>
                                                <div class="col-md-2">
                                                    <input type="text" class="form-control" name="Condition[min_amount]"
                                                           value="<?php echo isset($condition['min_amount'])?$condition['min_amount']:0;?>">
                                                </div>
                                            </div>

                                            <input type="hidden" value="1" name="Condition[all]" class="condition-all">

                                            <div id="condition" class="tabbable tabbable-tabdrop"
                                                 style="display: <?php echo (isset($condition['all'])&&$condition['all']==1)||!isset($condition['all'])?'none':'block';?>;">
                                                <ul class="nav nav-tabs">
                                                    <li class="active">
                                                        <a href="#tab2" data-toggle="tab" aria-expanded="true">品牌</a>
                                                    </li>
                                                    <li class="">
                                                        <a href="#tab1" data-toggle="tab" aria-expanded="false">分类</a>
                                                    </li>
                                                    <li class="">
                                                        <a href="#tab3" data-toggle="tab" aria-expanded="false">商品</a>
                                                    </li>
                                                </ul>
                                                <div class="tab-content scroller" style="height:300px" >
                                                    <div class="tab-pane active" id="tab2">
                                                        <select multiple="multiple" class="multi-select" id="my_multi_select1" >
                                                            <?php //foreach ($brand_list as $v):?>
                                                            <option value="<?php //echo $v['brand_id'];?>"
                                                            <?php //echo isset($condition['brand'])&&in_array($v['brand_id'], $condition['brand'])?'selected':'';?>><?php //echo $v['brand_name'];?></option>
                                                            <?php //endforeach;?>
                                                        </select>
                                                        <?php if (isset($condition['brand'])):?>
                                                        <?php foreach($condition['brand'] as $v):?>
                                                        <input type="hidden" id="brand-opt-<?php echo $v;?>" value="<?php echo $v;?>" name="Condition[brand][]">
                                                        <?php endforeach;?>
                                                        <?php endif;?>
                                                    </div>
                                                    <div class="tab-pane" id="tab1">
                                                        <div id="tree_2" class="tree-demo"> </div>
                                                        <input type="hidden" name="Condition[cat]" class="condition-cat"
                                                               value="<?php echo isset($condition['cat'])?implode(',', $condition['cat']):'';?>">
                                                    </div>
                                                    <div class="tab-pane" id="tab3">
                                                        <select multiple data-role="tagsinput" class="goods-list" name="Condition[Goods][]" id="tags">
                                                            <?php if (isset($condition_goods) && $condition_goods):?>
                                                            <?php foreach ($condition_goods as $v):?>
                                                            <option value="<?php echo $v['goods_id'].'_'.$v['name'];?>" data-id="<?php echo $v['goods_id'];?>"><?php echo $v['name'];?></option>
                                                            <?php endforeach;?>
                                                            <?php endif;?>
                                                        </select>

                                                        <?php if (isset($condition_goods) && $condition_goods):?>
                                                        <?php foreach ($condition_goods as $v):?>
                                                        <input type="hidden" name="Condition[goods][]" id="tag-<?php echo $v['goods_id'];?>" value="<?php echo $v['goods_id'];?>">
                                                        <?php endforeach;?>
                                                        <?php endif;?>

                                                        <div class="margin-top-2">
                                                            <a href="javascript:;" class="btn btn-success goods-select"
                                                               data-target="#large" data-toggle="modal" type="button"
                                                               data-type="2" data-id="0">搜索</a>
                                                        </div>
                                                        <div class="modal fade bs-modal-lg" id="large" tabindex="-1" role="dialog" aria-hidden="true">
                                                            <div class="modal-dialog modal-lg">
                                                                <div class="modal-content">
                                                                    <div class="modal-header">
                                                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                                                                        <h4 class="modal-title">商品选择</h4>
                                                                    </div>
                                                                    <div class="modal-body">
                                                                    </div>
                                                                    <div class="modal-footer">
                                                                        <button type="button" class="btn dark btn-outline goods-one-close" data-dismiss="modal">关闭</button>
                                                                        <button type="button" class="btn green goods-one-sub">保存</button>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="portlet light">
                                    <div class="portlet-title">
                                        <div class="caption">
                                            <i class="icon-speech"></i>
                                            <span class="caption-subject bold uppercase"> 优惠方案</span>
                                        </div>
                                    </div>
                                    <div class="portlet-body">
                                        <div class="scroller" style="height:300px" data-rail-visible="1" data-rail-color="yellow" data-handle-color="#a1b2bd">
                                            <div class="form-group action-subtract"
                                                 style="display: <?php echo (isset($rule_row['reduce_type'])&&$rule_row['reduce_type']=='subtract')||!isset($rule_row['reduce_type'])?'flex':'none';?>">
                                                <label class="control-label col-md-3">订单减免金额：</label>
                                                <div class="col-md-2">
                                                    <input type="text" class="form-control" name="Action[order_amount]"
                                                           value="<?php echo isset($actions['order_amount'])?$actions['order_amount']:0;?>">
                                                </div>
                                            </div>
                                            <div class="form-group action-subtract"
                                                 style="display: <?php echo (isset($rule_row['reduce_type'])&&$rule_row['reduce_type']=='subtract')||!isset($rule_row['reduce_type'])?'flex':'none';?>">
                                                <label class="control-label col-md-3">商品减免金额：</label>
                                                <div class="col-md-2">
                                                    <input type="text" class="form-control" name="Action[goods_amount]"
                                                           value="<?php echo isset($actions['goods_amount'])?$actions['goods_amount']:0;?>">
                                                </div>
                                            </div>
                                            <div class="form-group action-freeship"
                                                 style="display: <?php echo isset($rule_row['reduce_type'])&&$rule_row['reduce_type']=='freeship'?'block':'none';?>">
                                                <label class="control-label col-md-3">物流结算金额：</label>
                                                <div class="col-md-2">
                                                    <input type="text" class="form-control" name="Action[ship_amount]" readonly
                                                           value="<?php echo isset($actions['ship_amount'])?$actions['ship_amount']:0;?>">
                                                </div>
                                            </div>
                                            <div class="form-group action-discount"
                                                 style="display: <?php echo isset($rule_row['reduce_type'])&&$rule_row['reduce_type']=='discount'?'block':'none';?>">
                                                <label class="control-label col-md-3">折扣：</label>
                                                <div class="col-md-2">
                                                    <input type="text" class="form-control" name="Action[discount]"
                                                           value="<?php echo isset($actions['discount'])?$actions['discount']:1;?>"><span><p>范围：0~1</p></span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-actions">
                        <div class="row">
                            <div class="offset-md-3 col-md-4">
                                <button style="text-align: right" type="submit" class="btn btn-success">提交</button>
                                <button type="reset" class="btn btn-default">取消</button>
                            </div>
                        </div>
                    </div>
                    {{csrf_field()}}
                </form>
            </div>
        </div>
    </div>
</div>




@section('script')
    <script src="{{asset('admin-lte/plugins/datatables/jquery.dataTables.js')}}"></script>
    <script src="{{asset('admin-lte/plugins/datatables/dataTables.bootstrap4.js')}}"></script>

    {{--jqueryVlaition插件--}}
    <script src="{{asset('admin-lte/plugins/jquery-validation/js/jquery.validate.js')}}"></script>
    <script src="{{asset('js/spu/form-validate.js')}}"></script>

    {{--layer插件--}}
    <script src="{{asset("admin-lte/plugins/layer-v3.1.1/layer.js")}}" type="text/javascript"></script>
    <script>
        //表单验证
        validate(
            {
                'Rule[title]':{'required': true},
                'Rule[brief]':{'required': true},
                'Rule[type]':{'required': true},
                'Rule[reduce_type]':{'required': true}
            },
            {
                'Rule[title]':{'required': '标题不能为空'},
                'Rule[brief]':{'required': '描述不能为空'},
                'Rule[type]':{'required': '规则类型不能为空'},
                'Rule[reduce_type]':{'required': '优惠类型不能为空'}
            }
        );

        //促销类型
        $('.activity-type').change(function(){
            var val = $(this).val();
            if (val == 'subtract') {
                $('.action-subtract').show();
                $('.action-freeship').hide();
                $('.action-discount').hide();
            } else if (val == 'freeship') {
                $('.action-subtract').hide();
                $('.action-freeship').show();
                $('.action-discount').hide();
            } else if (val == 'discount') {
                $('.action-subtract').hide();
                $('.action-freeship').hide();
                $('.action-discount').show();
            }
        });

        //是否全场
        $('.switch-radio1').on('switchChange.bootstrapSwitch', function(event, state) {
            if (state == false) {
                $('#condition').show();
                $('.condition-all').val(0);
            } else {
                $('#condition').hide();
                $('.condition-all').val(1);
            }
        });

        //商品选择
        $('.goods-select').on('click',function(){
            App.blockUI({
                target: '#portlet',
                animate: true
            });

            window.setTimeout(function() {
                App.unblockUI('#portlet');
            }, 2000);
            var selectType = $(this).attr('data-type');
            $.post(
                '?r=goods/goods/default',
                {
                    'selectType':selectType,
                    'rule_id':$('.rule_id').val(),
                    '_csrf': $('.request-csrf').val()
                },
                function (res) {
                    $('.modal-body').html(res);
                }
            );
        });
        //商品选择确认
        $('.goods-one-sub').on('click',function(){
            var obj = $('.goods:checked');

            obj.each(function(){
                var goods_id = $(this).val();
                var goods_name = $(this).parent().next().html();
                $('.bootstrap-tagsinput').prepend('<span class="tag label label-info">'+goods_id+'_'+goods_name+'<span class="tag-remove" data-id="'+goods_id+'"></span></span><br>');
                $('#tab3').append('<input type="hidden" name="Condition[goods][]" id="tag-'+goods_id+'" value="'+goods_id+'">');
            });
            $('.goods-one-close').click();
        });

        // $('#tags').tagsinput()
        $('#tags').on('itemRemoved', function(event) {
            var tag = event.item;
            var goods_id = tag.split('_')[0];
            $('#tag-'+goods_id).remove();
        });

        $('#tab3').on('click', '.tag-remove',function(){
            var goods_id = $(this).attr('data-id');
            $(this).parent().next().remove();
            $(this).parent().remove();
            $('#tag-'+goods_id).remove();
        });
    </script>
@stop